package com.csj.mvvm.ui.base

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.fragment.app.FragmentActivity
import com.google.gson.Gson

object StartAct {
    /**
     * 启动Activity
     */
    inline fun <reified T : FragmentActivity> FragmentActivity.onStart(context: Context) {
        startActivity(Intent(context, T::class.java))
    }

    inline fun <reified T : FragmentActivity> FragmentActivity.onStart(
        context: Context,
        intents: Intent?
    ) {
        val mIntent = Intent(context, T::class.java)
        if (intents != null) {
            mIntent.putExtras(intents)
        }
        startActivity(mIntent)
    }

    inline fun <reified T : FragmentActivity> FragmentActivity.onStartResult(
        context: Context,
        requestCode: Int
    ) {
        startActivityForResult(Intent(context, T::class.java), requestCode)
    }

    inline fun <reified T : FragmentActivity> FragmentActivity.onStartResult(
        context: Context,
        requestCode: Int,
        intents: Intent?
    ) {
        val mIntent = Intent(context, T::class.java)
        if (intents != null) {
            mIntent.putExtras(intents)
        }
        startActivityForResult(mIntent, requestCode)
    }

}


fun View.cornerRadiusBg(color: Int = Color.WHITE, cornerRadius: Int = 15) {
    background = GradientDrawable().apply {
        setColor(color)
        setCornerRadius(cornerRadius.toFloat())
    }
}

inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, T::class.java)

fun Any?.toString(): String {
    if (this == null) {
        return ""
    }
    return toString()
}


